package com.shop.controller;

import com.shop.common.SetMeta;
import com.shop.common.page.UserPage;
import com.shop.entity.Manager;
import com.shop.entity.vo.ManagerRoleVo;
import com.shop.exception.UserException;
import com.shop.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class ManagerController {
    @Autowired
    public ManagerService managerService;

    /**
     * 分页查询
     * @param pagenum
     * @param pagesize
     * @return
     */
    @GetMapping("/users")
    public SetMeta<UserPage<List<ManagerRoleVo>>> getUsers(@RequestParam(required = false) String query,
                                                               @RequestParam Integer pagenum,
                                                               @RequestParam Integer pagesize) {
        return managerService.selectPage(query,pagenum,pagesize);
    }
    /**
     * 添加用户
     * @param managerRoleVo
     * @return
     */
    @PostMapping("/users")
    public SetMeta<ManagerRoleVo> insert(@RequestBody ManagerRoleVo managerRoleVo) {
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = managerService.selectByUsername(managerRoleVo.getUsername()).getMeta().getStatus() == 200 ? managerService.insert(managerRoleVo) : managerService.selectByUsername(managerRoleVo.getUsername());
        } catch (UserException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 修改用户状态
     * @param uid
     * @param type
     * @return
     */
    @PutMapping("/users/{uid}/state/{type}")
    public SetMeta<Manager> updateType(@PathVariable Integer uid,
                                       @PathVariable Boolean type) {
        return managerService.updateType(uid, type);
    }

    /**
     * 根据ID查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public SetMeta<Manager> selectOne(@PathVariable Integer id) {
        return managerService.selectOne(id);
    }

    /**
     * 编辑用户提交
     * @param id
     * @param email
     * @param mobile
     * @return
     */
    @PutMapping("/users/{id}")
    public SetMeta<Manager> updateUser(@PathVariable Integer id,
                                       @RequestParam(required = false) String email,
                                       @RequestParam(required = false) String mobile) {
        return managerService.updateUser(id, email, mobile);
    }

    /**
     * 删除单个用户
     * @param id
     * @return
     */
    @DeleteMapping("/users/{id}")
    public SetMeta<Manager> deleteUser(@PathVariable Integer id) {
        return managerService.deleteUser(id);
    }

    /**
     * 分配用户角色
     * @param id
     * @param manager
     * @return
     */
    @PutMapping("/users/{id}/role")
    public SetMeta<Manager> updateRole(@PathVariable Integer id,
                                       @RequestBody Manager manager) {
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = managerService.updateRole(id, manager);
        } catch (UserException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }
}
